package cxydmmszl.chapter02.t015;

/**
 * <li style="color: red;">Prob</li>
 * 反转链表和双向链表
 * <li style="color: red;">Desc</li>
 * 分别实现反转单向链表和反转双向链表的函数。
 * <li style="color: red;">Link</li> CD107
 *
 * @author habitplus
 * @since 2021-08-28 10:33
 */
public class Main {
    private static class Node {
        int val;
        Node next;

        public Node(int val) {
            this.val = val;
        }
    }

    private static class DoubleNode {
        int val;
        DoubleNode prev;
        DoubleNode next;

        public DoubleNode(int val) {
            this.val = val;
        }
    }

    public Node reverseLinkedList(Node head) {
        Node pre = null;
        Node t;
        while (head != null) {
            t = head.next;
            head.next = pre;
            pre = head;
            head = t;
        }
        return pre;
    }

    public DoubleNode reverseDoubleLinkedList(DoubleNode head) {
        DoubleNode pre = null;
        DoubleNode nxt;
        while (head != null) {
            nxt = head.next;
            head.next = pre;
            head.prev = nxt;
            pre = head;
            head = nxt;
        }
        return pre;
    }
}
